﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Collections;
using System.Web.Security;
using System.Security.Principal;
using DalatCooking.Core;
using DalatCooking.Core.Helper;
public partial class admin_admin : System.Web.UI.MasterPage
{
    public Person p;
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected override void OnInit(EventArgs e)
    {

        p = Session.GetPersonLogged();
        if (p != null)
        {

            //   bool rs = SecurityServices.HasPermission("superuser", p.PersonId, SecurityServices.TypeAudit.SuperUser);

            //if (!p.IsInRole("Editor") )
            //{
            //    Session.Abandon();
            //    Response.Redirect("~/Account/Login.aspx");
            //}
            

            lblUserName.Text = p.Profile.DisplayName;
        }
        else Response.Redirect("~/Account/Login.aspx");
        base.OnInit(e);
    }
    protected void logout_Click(object sender, EventArgs e)
    {
        System.Web.Security.FormsAuthentication.SignOut();
        Session.Abandon();
        Response.Redirect("~/Account/Login.aspx");
       
    }
    protected void clearcache_Click(object sender, EventArgs e)
    {
        List<string> keys = new List<string>();
        // retrieve application Cache enumerator
        IDictionaryEnumerator enumerator = Cache.GetEnumerator();
        // copy all keys that currently exist in Cache
        while (enumerator.MoveNext())
        {
            keys.Add(enumerator.Key.ToString());
        }
        // delete every key from cache
        for (int i = 0; i < keys.Count; i++)
        {
            Cache.Remove(keys[i]);
        }
        Response.Redirect("/admin/Default.aspx");
    }
    public void BindMenu()
    {
        var db = MenuManager.FindByGroup("adminmenu");
        // Response.Write(db.Count);
        Response.Write("<ul id=\"ulMenu\" class=\"toprounded\">");
        foreach (
             var adminNode in
                db.Where(adminNode =>
                         this.Request.RawUrl.ToUpperInvariant().Contains("/ADMIN/") && adminNode.IsActive == true))
        {
            var cls = "";
            var nodeUrl = adminNode.Url;
            var startIndx = nodeUrl.LastIndexOf("/admin/") > 0 ? nodeUrl.LastIndexOf("/admin/") : 0;
            var endIndx = adminNode.Url.LastIndexOf(".") > 0 ? adminNode.Url.LastIndexOf(".") : adminNode.Url.Length;
            var nodeDir = adminNode.Url.Substring(startIndx, endIndx - startIndx);

            if (Request.RawUrl.IndexOf(nodeUrl, StringComparison.OrdinalIgnoreCase) != -1 ||
                (Request.RawUrl.IndexOf(nodeDir, StringComparison.OrdinalIgnoreCase) != -1) &&
                !nodeDir.EndsWith("/admin"))
            {
                cls = "current";
            }

            // if "page" has its own subfolder (comments, extensions etc.) 
            // select parent tab when navigating through child tabs
            startIndx = nodeDir.LastIndexOf("/");
            if (startIndx > 0)
            {
                nodeDir = nodeDir.Substring(0, startIndx);
                if (Request.RawUrl.ToLower().Contains(nodeDir.ToLower() + "/") && nodeDir.Substring(1, nodeDir.Length - 1).IndexOf("/") > 0)
                {
                    cls = "current";
                }
            }
            Response.Write("<li><a href=\"" + adminNode.Url + "\" class=\"" + cls + "\"><span>" + adminNode.Name + "</span></a></li>");
        }
        Response.Write("</ul>");
    }
}
